package one.stand.controller.order;

import com.jmp.api.dto.AddressDefaultRequest;
import com.jmp.api.dto.AddressListResponse;
import com.jmp.api.dto.AddressRemoveRequest;
import com.jmp.api.dto.AddressSaveRequest;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.dto.*;
import com.jmp.feign.AddressFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import one.stand.dto.ConvertUtils;
import one.stand.model.ResultModel;
import one.stand.util.AssertUtil;
import one.stand.util.RequestContext;
import one.stand.util.RequestContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author cjr
 */
@Api(description = "收货地址管理")
@RestController
@RequestMapping("address")
public class AddressController {
    @Autowired
    private AddressFeign addressFeign;

    @ApiOperation(value = "1列表")
    @GetMapping("/list")
    @SecurityAuthCheck
    public ResultModel<AddressListResponse> list() {
        final RequestContext.User optUser = RequestContextHolder.getRequestContext().getUser();
        ResultModel<AddressListVo> resultModel = addressFeign.list(AddressListDto.builder().userId(optUser.getUserId()).build());
        AssertUtil.isTrue(resultModel);

        List<AddressListResponse.Address> addressList = ConvertUtils.list(resultModel.getData().getAddressList(), address -> {
            return AddressListResponse.Address.builder()
                    .addressId(address.getAddressId())
                    .defaultFlag(address.getIsDefault())
                    .locationAddress(address.getLocationAddress())
                    .detailedAddress(address.getDetailedAddress())
                    .receiveName(address.getReceiveName())
                    .receivePhone(address.getReceivePhone())
                    .cityName(address.getCityName())
                    .latitude(address.getLatitude())
                    .longitude(address.getLongitude())
                    .build();
        });
        AddressListResponse response = AddressListResponse.builder()
                .addressList(addressList)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation(value = "2保存")
    @PostMapping("/save")
    @SecurityAuthCheck
    public ResultModel addAddress(@RequestAttribute AddressSaveRequest request) {
        final RequestContext.User optUser = RequestContextHolder.getRequestContext().getUser();
        ResultModel resultModel = addressFeign.save(AddressSaveDto.builder()
                .addressId(request.getAddressId())
                .isDefault(request.getDefaultFlag())
                .locationAddress(request.getLocationAddress())
                .detailedAddress(request.getDetailedAddress())
                .receiveName(request.getReceiveName())
                .receivePhone(request.getReceivePhone())
                .userId(optUser.getUserId())
                .cityName(request.getCityName())
                .latitude(request.getLatitude())
                .longitude(request.getLongitude())
                .build());
        AssertUtil.isTrue(resultModel);

        return ResultModel.success();
    }

    @ApiOperation(value = "2删除")
    @PostMapping("/remove")
    @SecurityAuthCheck
    public ResultModel delAddress(@RequestAttribute AddressRemoveRequest request) {
        ResultModel resultModel = addressFeign.remove(AddressRemoveDto.builder()
                .addressId(request.getAddressId())
                .build());
        AssertUtil.isTrue(resultModel);
        return ResultModel.success();
    }

    @ApiOperation(value = "2设置为默认")
    @PostMapping("/default")
    @SecurityAuthCheck
    public ResultModel isdefult(@RequestAttribute AddressDefaultRequest request) {
        ResultModel resultModel = addressFeign.defaultSet(AddressDefaultSetDto.builder()
                .addressId(request.getAddressId())
                .build());
        AssertUtil.isTrue(resultModel);
        return ResultModel.success();
    }
}
